﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.ComponentModel;

namespace ClientLib.Common
{

 public   class DynamicState:IComparable<DynamicState>,INotifyPropertyChanged
 {
     #region
        private string domainName = "";
        private string agentName = "";
        private string devID = "";
        private string stateName = "";
        private string deviceType = "";
        private string viewType = "";
        private int timeInterval = 1000;
        private int changeThreshold = 0;
        private bool setAble = true;

        AttributeValue stateValue;
     #endregion

        #region ////属性集合设置
        /****************************
         状态名 属性
         ***************************/
        public string StateName
        {
            get { return stateName; }
            set { stateName = value; }
        }

        public string DomainName
        {
            get { return domainName; }
            set { domainName = value; }
        }


        public string AgentName
        {
            get { return agentName; }
            set { agentName = value; }
        }

        public string DevID
        {
            get { return devID; }
            set { devID = value; }
        }

        public string ViewType
        {
            get { return viewType; }
            set { viewType = value; }
        }

        public int TimeInterval
        {
            get { return timeInterval; }
            set { timeInterval = value; }
        }

        public int ChangeThreshold
        {
            get { return changeThreshold; }
            set { changeThreshold = value; }
        }

        public bool SetAble
        {
            get { return setAble; }
            set { setAble = value; }
        }
        /****************************
         状态值 属性
         ***************************/
        public AttributeValue StateValue
        {
            get { return stateValue; }
            set {
                
                if (stateValue != value)
                {
                    stateValue = value;
                    OnPropertyChanged("StateValue");
                }
                }
        }
        #endregion

        #region///自定义类函数
        /****************************
         构造函数2
         ***************************/
        public DynamicState(string stateName,AttributeValue initVal)
        {
            this.stateName = stateName;
            this.stateValue = initVal;
        }

        public DynamicState(string st_Name,string dev_id,string mon_id,string dev_type)
        {
            this.AgentName = mon_id;
            this.devID = dev_id;
            this.stateName = st_Name;
            this.deviceType = dev_type;
            this.stateValue = null;
        }

        
        public DynamicState(string st_Name, string dev_id, string mon_id, string dev_type, string view_type,string domain_name,int change,int time_in )
        {
            this.AgentName = mon_id;
            this.devID = dev_id;
            this.stateName = st_Name;
            this.deviceType = dev_type;
            this.domainName = domain_name;
            this.viewType = view_type;
            this.changeThreshold = change;
            this.timeInterval = time_in;

            this.stateValue = null;
        }
        /****************************
         设置状态值
         ***************************/
        public bool setVal(DataType type, object value)
        {
            try
            {
                AttributeValue av = new AttributeValue(type, value);
                this.stateValue = av;
            }
            catch (Exception e)
            {
                Console.WriteLine(e.StackTrace+":"+e.Message);
                return false;
            }
            return true;            
        }

        /****************************
         设置状态值
         ***************************/
        public void setVal(object value)
        {
            try
            {                
                this.stateValue.setDataValue(value);
            }
            catch (Exception e)
            {
                System.Diagnostics.Debug.WriteLine(e.StackTrace + ":" + e.Message);///////////
                throw new ApplicationException("设置状态值出错："+e.Message);                
            }
        }

        /****************************
         设置状态名
         ***************************/
        public void setName(string name)
        {
            this.stateName = name;
        }

        #endregion
        /****************************
        实现ICompareble<>接口
        ***************************/
        #region
        public bool Equals(DynamicState other)
        {
            try
            {
                if ((this.stateName == other.stateName) & (this.stateValue == other.stateValue))
                {
                    return true;
                }
                else
                {
                    return false;
                }
            }
            catch (Exception e)
            {
                throw e;
            }
        }

        public int CompareTo(DynamicState other)
        {
            try
            {
                if (this.stateName != other.stateName)
                {
                    return stateName.CompareTo(other.stateName);
                }
                else
                {
                    return this.stateValue.CompareTo(other.stateValue);
                }
            }
            catch (Exception e)
            {
                throw e;
            }
        }
        #endregion


        /****************************
        实现INotifyChanged接口
        ***************************/
        #region
        public event PropertyChangedEventHandler PropertyChanged;

        private void OnPropertyChanged(string propertyName)
        {
            PropertyChangedEventHandler handler = this.PropertyChanged;
            if (handler != null)
            {
                handler(this, new PropertyChangedEventArgs(propertyName));
            }
        }
        #endregion


        public override string ToString()
        {
            if (stateValue != null)
            {
                return stateValue.ToString();
            }
            else
            {
                return "the value is Empty";
            }
        }
    }
}
